package org.tity.test.network;

import javax.wsdl.*;
import javax.wsdl.extensions.*;
import javax.wsdl.factory.*;
import javax.wsdl.xml.*;
import javax.xml.namespace.QName;
import java.util.*;

import org.tity.commons.code.CodeBuilder;
import org.w3c.dom.*;

public class NavigatingWSDL {
	
//	private static String wsdl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
//	private static String wsdl = "https://open.auxgroup.com/api/sms/soap/message?wsdl";
//	private static String wsdl = "http://10.88.20.82:8001/soap/message?wsdl";
	
//	private static String wsdl = "http://127.0.0.1:8001/soap/message?wsdl";
	private static String wsdl = "http://127.0.0.1:8001/soap/sendMessage?wsdl";
	
	
	public static void main(String[] args) {
		try {
			WSDLFactory factory = WSDLFactory.newInstance();
			WSDLReader reader = factory.newWSDLReader();
			reader.setFeature("javax.wsdl.verbose", true);
			reader.setFeature("javax.wsdl.importDocuments", true);
			Definition def = reader.readWSDL(wsdl);
			
			CodeBuilder code=new CodeBuilder();
			code.ln(wsdl);
			
			Map services=def.getServices();
			for (Object sName : services.keySet()) {
				
				QName qName=(QName)sName;
				code.ln(1,"service : "+qName.getLocalPart());
				Service service=(Service)services.get(sName);
				System.out.println(service.getQName().getLocalPart());
				Map ports=service.getPorts();
				for (Object portName : ports.keySet()) {
					code.ln(2,"port : "+portName);
					Port port = (Port)ports.get(portName);
					Binding binding = port.getBinding();
					PortType portType = binding.getPortType();
					List operations = portType.getOperations();
					Iterator operIter = operations.iterator();
					while (operIter.hasNext()) {
						Operation operation = (Operation) operIter.next();
						if (!operation.isUndefined()) {
							code.ln(3,"operation : "+operation.getName());
						
							Map parts=operation.getInput().getMessage().getParts();
							for (Object partName : parts.keySet()) {
								Part part = (Part) parts.get(partName);
								
								System.out.println(partName);
								QName typeName=part.getTypeName();
								String type="";
								if(typeName!=null)
								{
									type=" : "+typeName.getLocalPart();
								}
								QName n=part.getElementName();
								List el=part.getExtensibilityElements();
								
								QName tn=part.getTypeName();
								
								part.getExtensionAttributes();
								Message m=def.getMessage(n);
								Map ps=m.getParts();
								
								Types types=def.getTypes();
								 
								code.ln(4,"param : "+part.getName()+type);
							 
							}
						}
					}
				}
			}
			
			 
			
//			Map messages = def.getMessages();
//			Iterator msgIter = messages.values().iterator();
//			while (msgIter.hasNext()) {
//				Message msg = (Message) msgIter.next();
//				if (!msg.isUndefined()) {
//					System.out.println(msg.getQName().getLocalPart());
//					Iterator partIter = msg.getParts().values().iterator();
//					while (partIter.hasNext()) {
//						Part part = (Part) partIter.next();
//						code.ln("param : "+part.getName());
////						System.out.print("parameter name:" + part.getName() + "t");
////						System.out.println("parameter type:" + part.getTypeName().getLocalPart());
//					}
//				}
//			}
			
			System.out.println(code.toString());
			
//			// 解析服务名
//			System.out.println("----------");
//			System.out.println("nService Name:");
//			String tns = "http://WebXml.com.cn/";
//			Service service = def.getService(new QName(tns, "getWeatherbyCityName"));
//			System.out.println(service.getQName().getLocalPart());
//			// 解析接口方法名
//			System.out.println("nOperation Name:");
//			Port port = service.getPort("getWeatherbyCityName");
//			Binding binding = port.getBinding();
//			PortType portType = binding.getPortType();
//			List operations = portType.getOperations();
//			Iterator operIter = operations.iterator();
//			while (operIter.hasNext()) {
//				Operation operation = (Operation) operIter.next();
//				if (!operation.isUndefined()) {
//					System.out.println(operation.getName());
//				}
//			}
//			// 解析消息，输入输出
//			System.out.println("nMessages:");
//			Map messages = def.getMessages();
//			Iterator msgIter = messages.values().iterator();
//			while (msgIter.hasNext()) {
//				Message msg = (Message) msgIter.next();
//				if (!msg.isUndefined()) {
//					System.out.println(msg.getQName().getLocalPart());
//					Iterator partIter = msg.getParts().values().iterator();
//					while (partIter.hasNext()) {
//						Part part = (Part) partIter.next();
//						System.out.print("parameter name:" + part.getName() + "t");
//						System.out.println("parameter type:" + part.getTypeName().getLocalPart());
//					}
//				}
//			}
//			// 解析服务地址
//			System.out.println("nService location:");
//			List l = port.getExtensibilityElements();
//			ExtensibilityElement element = (ExtensibilityElement) l.get(0);
//			String s = element.toString();
//			System.out.println(s.substring(s.indexOf("location")));
//			System.out.println("---------");

		} catch (WSDLException e) {
			e.printStackTrace();
		}
	}
}
